package com.xdhl.fu.fim.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.utils.GaodMapUtils;
import com.xdhl.fu.fim.service.IFlightAirportInfoService;
import com.xdhl.fu.fim.service.IFlightInfoService;
import com.xdhl.fu.flight.info.api.domain.FlightAirportInfo;
import com.xdhl.fu.flight.info.api.domain.FlightInfo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 检查空距离的数据，然后计算并更新距离
 *
 * @author 朱力维
 * @time 2022/6/3 19:39
 */
@Component
public class CheckNoDistanceFlightInfoRunner implements CommandLineRunner {

    @Resource
    private IFlightInfoService flightInfoService;

    @Resource
    private IFlightAirportInfoService flightAirportInfoService;

    @Override
    public void run(String... args) throws Exception {

        LambdaQueryWrapper<FlightInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(FlightInfo::getDistance);
        List<FlightInfo> list = flightInfoService.list(wrapper);

        for (FlightInfo flightInfo : list) {
            LambdaQueryWrapper<FlightAirportInfo> firstStartAirportWrapper = new LambdaQueryWrapper<>();
            firstStartAirportWrapper.eq(FlightAirportInfo::getCityCode, flightInfo.getStartStation());
            FlightAirportInfo firstStart = flightAirportInfoService.getOne(firstStartAirportWrapper);
            LambdaQueryWrapper<FlightAirportInfo> firstEndAirportWrapper = new LambdaQueryWrapper<>();
            firstEndAirportWrapper.eq(FlightAirportInfo::getCityCode, flightInfo.getEndStation());
            FlightAirportInfo firstEnd = flightAirportInfoService.getOne(firstEndAirportWrapper);
            String firstStartCityName = firstStart.getCityName();
            String firstStartAddress = firstStart.getLabel().split(",")[0];
            String firstEndCityName = firstEnd.getCityName();
            String firstEndAddress = firstEnd.getLabel().split(",")[0];

            try {
                flightInfo.setDistance(GaodMapUtils.getDistance(firstStartCityName, firstStartAddress, firstEndCityName, firstEndAddress));
            } catch (Exception e) {
                continue;
            }
            flightInfoService.updateById(flightInfo);

        }
    }
}
